//
//  MTPageViewController.m
//  ebook
//
//  Created by 吴超飞 on 16/4/15.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "MTPageViewController.h"

@interface MTPageViewController ()

@end

@implementation MTPageViewController

- (instancetype)initWithTransitionStyle:(UIPageViewControllerTransitionStyle)style navigationOrientation:(UIPageViewControllerNavigationOrientation)navigationOrientation options:(NSDictionary<NSString *,id> *)options
{
    self = [super initWithTransitionStyle:style navigationOrientation:navigationOrientation options:options];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)loadView{
    [super loadView];
}

///*解决UIPageViewControllerTransitionStyleScroll中-[UIPageViewController queuingScrollView:didBailoutOfScrollAndRevealedView:]的问题*/
//- (void) setViewControllers:(NSArray*)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^)(BOOL))completion {
//
//    @weakify(self)
//    [self.pageViewController setViewControllers:@[targetViewController]
//                                      direction:direction
//                                       animated:animated
//                                     completion:^(BOOL finished)
//     {
//         @strongify(self)
//         if (finished) {
//             dispatch_async(dispatch_get_main_queue(), ^
//                            {
//                                if (self) {
//                                    [self.pageViewController setViewControllers:@[self.pages[index]] direction:direction animated:NO completion:NULL];
//                                    // bug fix for uipageview controller
//                                }
//                            });
//         }
//     }];
//}

/*[MTPageViewController queuingScrollView:didEndManualScroll:toRevealView:direction:animated:didFinish:didComplete:]*/
- (void) setViewControllers:(NSArray*)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^)(BOOL))completion {
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        if (!animated) {
            [super setViewControllers:viewControllers direction:direction animated:NO completion:completion];
            return;
        }
        
        [super setViewControllers:viewControllers direction:direction animated:YES completion:^(BOOL finished){
            
            if (finished) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [super setViewControllers:viewControllers direction:direction animated:NO completion:completion];
                });
            } else {
                if (completion != NULL) {
                    completion(finished);
                }
            }
        }];
        
    });
}

@end
